#include "PathInfoReader.h"
#include <QDebug>
PathInfoReader::PathInfoReader(const QString& path):xml_file_(path)
{

}
PathInfoReader::PathInfoReader():xml_file_("path.xml")
{
	
}

PathInfoReader::~PathInfoReader(void)
{	
}

void PathInfoReader::getPathInfos(std::vector<PathInfo>& vec )
{
	
	int ret = xml_file_.open(QIODevice::ReadOnly|QIODevice::Text);

	xml_doc_.setContent(&xml_file_,false);
	QDomElement root = xml_doc_.documentElement();
	
	for(int i=0;i<root.childNodes().count();++i)
	{
		QDomNode node = root.childNodes().at(i);
		if (node.hasChildNodes())
		{
			PathInfo info;
			for(int j=0;j<node.childNodes().count();++j)
			{
				QDomNode childNode = node.childNodes().at(j);
				if (childNode.nodeName()=="name")
				{
					info.name = childNode.firstChild().nodeValue();
				}
				else if(childNode.nodeName()=="path")
				{
					info.path = childNode.firstChild().nodeValue();
				}
			}
			vec.push_back(info);
		}

	}
	xml_file_.close();
}
void PathInfoReader::writePathInfo(const PathInfo& pathInfo)
{
	xml_file_.open(QIODevice::ReadWrite|QIODevice::Text|QIODevice::Truncate);
	xml_doc_.setContent(&xml_file_,false);

	QDomProcessingInstruction node_pi_=xml_doc_.createProcessingInstruction("xml","version=\"1.0\" encoding=\"utf-8\"");
	QDomNode rN = xml_doc_.appendChild(node_pi_);
	QDomElement root = xml_doc_.createElement("filemanager");
	xml_doc_.appendChild(root);
	QDomElement node = xml_doc_.createElement("entry");
	
	QDomElement nameElement = xml_doc_.createElement("name");
	QDomElement pathElement = xml_doc_.createElement("path");
	
	node.appendChild(nameElement);
	node.appendChild(pathElement);

	QDomText node_text_name = xml_doc_.createTextNode(pathInfo.name);
	QDomText node_text_path = xml_doc_.createTextNode(pathInfo.path);

	nameElement.appendChild(node_text_name);
	pathElement.appendChild(node_text_path);

	root.appendChild(node);

	QTextStream out(&xml_file_);
	xml_doc_.save(out,4);
	xml_file_.close();
}
void PathInfoReader::setConfigPath(const QString& path)
{
	xml_path_ = path;
}